{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 前言 : \n",
    "\n",
    "1.1.0 是对于QA_Market的一个小规模重构,区别以往的订单,成交模式, 故更新一个小版本\n",
    "\n",
    "本次的更新主要有:\n",
    "\n",
    "\n",
    "[QAData]\n",
    "\n",
    "1. 修改了采样函数的写法\n",
    "2. 修复了tick采样成60min的bug\n",
    "3. 修复了因为multiindex导致的QA_DataStruct.to_json方法缺失 datetime/code 字段的问题\n",
    "\n",
    "[QAMARKET]\n",
    "\n",
    "\n",
    "1. 增加对于实盘易的支持\n",
    "2. 将一些基础解析字段挪至基类QABroker中\n",
    "3. 基于QAMarket创建的broker/order线程全部变成后台线程\n",
    "4. QA_OrderHandler 增加持久化 订单/成交单部分\n",
    "5. 修改QAOrder 变成有限状态机, 通过状态机的动作自动改变Order的状态\n",
    "6. 修改QABacktest_Broker 适配新的order类\n",
    "7. 优化QABroker的状态显示\n",
    "8. 将撮合缓存字典重构进QADealer, dealer加入 deal_message(dict) 和deal_df(dataframe),并增加dealer的settle函数\n",
    "9. 大量更新QABacktest_Broker方法,使之适配新版本回测\n",
    "10. 适配实盘,增加字段解析 trade_towards_cn_en, order_status_cn_en\n",
    "11. 修改order的撮合机制, 使用创建order时的callback回调来更新账户\n",
    "\n",
    "\n",
    "[QAEngine]\n",
    "\n",
    "1. QAThread 初始化增加 daemon选项, 用于创建守护线程\n",
    "\n",
    "[QAARP]\n",
    "\n",
    "1. QA_Account 增加 cancel_order方法 撤单操作\n",
    "2. 重写QA_Account的receive_deal方法, 适配新版本更新账户\n",
    "3. 优化和修改QA_Portfolio 增加对于history/history_table的支持\n",
    "\n",
    "\n",
    "[QASU]\n",
    "\n",
    "1. save_orderhandler.py 增加 QA_SU_save_order/ QA_SU_save_deal 方法\n",
    "2. 增加对于运行时出错的容错处理\n",
    "\n",
    "[QAUtil]\n",
    "\n",
    "1. QADate_Trade 增加QA_util_get_order_datetime() 用于获取委托的真实日期 QA_util_get_trade_datetime() 用于获取成交的真实日期\n",
    "2. QA_Parameter 修改订单状态\n",
    "\n",
    "[QAWEB]\n",
    "\n",
    "1. QAWEB 增加查询股票名称的接口 http://ip:port/marketdata/stock/code?code=xxxxx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> start QUANTAXIS\n",
      "QUANTAXIS>> Welcome to QUANTAXIS, the Version is 1.1.0\n",
      "QUANTAXIS>>  \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      "  ``########`````##````````##``````````##`````````####````````##```##########````````#``````##``````###```##`````######`` \n",
      "  `##``````## ```##````````##`````````####````````##`##```````##```````##```````````###``````##````##`````##```##`````##` \n",
      "  ##````````##```##````````##````````##`##````````##``##``````##```````##``````````####```````#```##``````##```##``````## \n",
      "  ##````````##```##````````##```````##```##```````##```##`````##```````##`````````##`##```````##`##```````##````##``````` \n",
      "  ##````````##```##````````##``````##`````##``````##````##````##```````##````````##``###```````###````````##`````##`````` \n",
      "  ##````````##```##````````##``````##``````##`````##`````##```##```````##```````##````##```````###````````##``````###```` \n",
      "  ##````````##```##````````##`````##````````##````##``````##``##```````##``````##``````##`````##`##```````##````````##``` \n",
      "  ##````````##```##````````##````#############````##```````##`##```````##`````###########`````##``##``````##`````````##`` \n",
      "  ###```````##```##````````##```##```````````##```##```````##`##```````##````##`````````##```##```##``````##```##`````##` \n",
      "  `##``````###````##``````###``##`````````````##``##````````####```````##```##``````````##``###````##`````##````##`````## \n",
      "  ``#########``````########```##``````````````###`##``````````##```````##``##````````````##`##``````##````##`````###``### \n",
      "  ````````#####`````````````````````````````````````````````````````````````````````````````````````````````````````##``  \n",
      "  ``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      "  ``````````````````````````Copyright``yutiansut``2018``````QUANTITATIVE FINANCIAL FRAMEWORK````````````````````````````` \n",
      "  ``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " \n"
     ]
    }
   ],
   "source": [
    "import QUANTAXIS as QA\n",
    "import pandas as pd\n",
    "import threading\n",
    "import time\n",
    "import datetime\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "if QA.__version__<'1.1.0':\n",
    "    print('请升级quantaxis后再运行此教程')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 首先我们介绍QA_MARKET:\n",
    "\n",
    "1. 如何理解QA_MARKET:\n",
    "    QA_MARKET 是一个交易前置, 从使用的角度看,你可以把他理解成一个交易终端(比如同花顺/通达信)\n",
    "    \n",
    "    在去年准备对于MARKET类重构的时候,QA_MARKET的作用图如下:\n",
    "    \n",
    "    ![pic](http://pic.yutiansut.com/%E9%87%8D%E6%9E%84%E6%96%87%E6%A1%A3-%E5%B8%82%E5%9C%BA.png)\n",
    "    \n",
    "    \n",
    "    交易前置主要起的作用是:\n",
    "    \n",
    "    1. 向上接入不同的Broker\n",
    "    2. 向下接入不同的账户\n",
    "    \n",
    "    broker可以利用不同的接口, 接入不同的市场(股票/期货/港股/美股/模拟盘/回测)\n",
    "\n",
    "2. QA_MARKET的类构成:\n",
    "\n",
    "    QA_Market继承于 QA_Trade, QA_Trade 是一个交易前置基类, 其中包含了 QA_Engine(用于开启多线程的管理引擎), 以及各种基类方法\n",
    "\n",
    "3. QA_Market的主要方法:\n",
    "\n",
    "    - start() 启动交易前置\n",
    "    \n",
    "    - connect(broker) 连接broker  (等同于在同花顺里面选择不同的券商)\n",
    "    \n",
    "    - register(broker_name,broker) 注册一个broker(如果不是官方支持的broker)\n",
    "    \n",
    "    - login(account_cookie,account) 登陆账户\n",
    "    \n",
    "    - login_out(account_cookie,account) 登出\n",
    "    \n",
    "    - get_account(account_cookie) 获得账户句柄\n",
    "    \n",
    "    - insert_order(account_cookie, amount, amount_model, time, code, price, order_model, towards, market_type, frequence, broker_name,     money=None)  插入订单\n",
    "    \n",
    "    - cancel_order(broker_name,account_cookie,order_id) 撤单\n",
    "    \n",
    "    - cancel_all(broker_name,account_cookie) 某一个账户订单全撤\n",
    "    \n",
    "    - query_assets(account_cookie) 查询某个账户的资产\n",
    "    \n",
    "    - query_position(account_cookie) 查询某个账户的持仓\n",
    "    \n",
    "    - query_cash(account_cookie) 查询某个账户的现金\n",
    "    \n",
    "    - query_data(broker_name, frequence, market_type, code, start, end=None) 查询历史行情数据\n",
    "    \n",
    "    - query_currentbar(broker_name, market_type, code) 查询当前行情\n",
    "    \n",
    "    - get_trading_day() 查询当前交易日期\n",
    "    \n",
    "    \n",
    "4. QA_Market的非阻塞事件\n",
    "\n",
    "    QA_Market的非阻塞事件由 QA_Engine, Engine 中管理着各个QA_Thread, 通过submit(QA_Task) 和submit_nowait(QA_Task)的方法将事件推送到处理队列中\n",
    "    \n",
    "    - upcoming_data(broker, data) 行情推送进交易前置, 分发至market下的所有账户中\n",
    "    \n",
    "    - settle(broker) 每日交易结算\n",
    "    \n",
    "    - insert_order 插入订单 可以在on_insert_order中写异步处理(需要继承)\n",
    "    \n",
    "    关于submit函数和QA_Task\n",
    "    \n",
    "    ![pic](http://pic.yutiansut.com/QUANTAXISEvent.png)\n",
    "    \n",
    "    你可以在QA_Task的QA_Event中自行写入回调来异步调用你的函数\n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "\n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
